/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.altius.web.controller;

import cc.altius.domain.CustomUserDetails;
import cc.altius.service.CustomerService;
import cc.altius.service.MessageService;
import cc.altius.service.SmsService;
import cc.altius.utils.DateUtils;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 *
 * @author Akil Mahimwala
 */
@Controller
public class IndexController {

    @Autowired
    private CustomerService customerService;
    @Autowired
    private SmsService smsService;
    @Autowired
    private MessageService messageService;

    @ModelAttribute("unreadCount")
    int getUnreadCountForMessage() {
        int curUser = ((CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserId();
        int unreadCount = this.messageService.getunReadMessageCountForCurUser(curUser);
        return unreadCount;
    }

    @RequestMapping(value = "/home/index.htm", method = RequestMethod.GET)
    public String GetPage(ModelMap modelMap) {
        CustomUserDetails user = ((CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal());
        String startDate = DateUtils.getCurrentDateString(DateUtils.IST, DateUtils.YMD);
        List<Map<String, Object>> lstSms = this.smsService.reportSmsStatus(startDate + " 00:00:00", startDate + " 23:59:59");
        modelMap.addAttribute("lstSms", lstSms);
        return "/home/index";
    }

    @RequestMapping(value = "/home/index.htm", method = RequestMethod.POST)
    public String showPage(@RequestParam(value = "mrNo1", required = false, defaultValue = "") String mrNo1,
            @RequestParam(value = "phone", required = false, defaultValue = "") String phone,
            @RequestParam(value = "customerName1", required = false, defaultValue = "") String customerName1,
            @RequestParam(value = "empId1", required = false, defaultValue = "") String empId1,
            ModelMap modelMap) {

        List<Map<String, Object>> customerlist = this.customerService.searchCustomer(mrNo1, phone, customerName1, empId1);
        modelMap.addAttribute("customerlist", customerlist);
        return "/home/index";
    }
}
